package com.hmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.ItemDto;
import com.hmall.common.dto.PageResult;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    /**
     * 分页查询接口
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult list(Integer page, Integer size) {
        //1.判断参数
        if(page==null){
            page=1;
        }
        if(size==null){
            size=5;
        }

        //2.查询分页操作
        IPage<Item> ipage=new Page<>(page,size);

        //查询条件，根据时间倒叙展示
        LambdaQueryWrapper<Item> queryWrapper= Wrappers.<Item>lambdaQuery();
        queryWrapper.orderByDesc(Item::getCreateTime);
        ipage = page(ipage, queryWrapper);

        //3.封装数据
        PageResult pageResult=new PageResult();
        pageResult.setTotal(ipage.getTotal());
        pageResult.setList(ipage.getRecords());


        return pageResult;
    }

    /**
     * 新增商品
     *
     * @param dto
     */
    @Override
    @Transactional
    public void saveItem(ItemDto dto) {
        //2.新增数据到mysql中
        Item item=new Item();

        //浅拷贝
        BeanUtils.copyProperties(dto,item);
        item.setPrice(item.getPrice()*100);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        save(item);


        //3.发送消息
        Map message=new HashMap<>();
        message.put("id",item.getId());
        message.put("status",1);
        rabbitTemplate.convertAndSend("down_or_up_exchange","downOrUp", JSON.toJSONString(message));

    }


    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 商品上下架功能
     *
     * @param id
     * @param status
     */
    @Override
    public void downOrUp(Long id, Integer status) {
        //1.判断是否有当前数据
        Item item = getById(id);
        if(item!=null){
            //2.直接修改状态
            item.setStatus(status);
            updateById(item);

            //3.发送消息
            Map message=new HashMap<>();
            message.put("id",id);
            message.put("status",status);
            rabbitTemplate.convertAndSend("down_or_up_exchange","downOrUp", JSON.toJSONString(message));
        }

    }

    /**
     * 根据id查询商品信息
     *
     * @param id
     * @return
     */
    @Override
    public ItemDto findById(Long id) {
        ItemDto itemDto=new ItemDto();
        Item item = getById(id);
        BeanUtils.copyProperties(item,itemDto);
        return itemDto;
    }
}
